﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;

namespace Classes
{
    // String dbConnection;

    class SqliteDB
    {
        //private string  dbConnection = String.Format("Data Source={0}");
        private DataSet ds;
        private SQLiteCommand  cmd;
        private DataTable dataTable;

        public SQLiteConnection  Conn(string dbConnection)
        {
            SQLiteConnection  conn = new SQLiteConnection(String.Format("Data Source={0}",dbConnection));
            return conn;
        }

        // public
        public bool SQLExec( SQLiteConnection Conn, string pSQL){

            try
            {
            if (Conn.State == ConnectionState.Closed)
                    Conn.Open();
            cmd = new SQLiteCommand(pSQL, Conn);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("Erro ao pesquisar dados.\r\nErro-> " + e.Message, "Erro",
                                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
        }

        public DataTable SQLSearch( SQLiteConnection Conn, string pSQL)
        {

   
           // Conn.Open();

            ds = new DataSet();

            try
            {

                if (Conn.State == ConnectionState.Closed)
                    Conn.Open();

                cmd = new SQLiteCommand(pSQL, Conn);
                ds = new DataSet();

                /* Executa a query */
                
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                dataTable = new DataTable();

                /* faz o retorno dentro do dataset */
                da.Fill(dataTable);
                da.Fill(ds);

                da.Dispose();
                return dataTable;
            }
            catch (Exception e)
            {
                MessageBox.Show("Erro ao pesquisar dados.\r\nErro-> " + e.Message, "Erro",
                                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return null;
            }
        }

    }

    //public SqliteDB(String inputFile)
    //{

    //    dbConnection = String.Format("Data Source={0}", inputFile);

    //}



}
